/**
 * Copyright (C) 2006 OQube 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 * author: Arnaud Bailly - abailly AT oqube DOT com
 * created: 14 juin 2006
 */
package speculoos.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A generic mapper that handle input collections and pass each member to
 * another mapper. This the speculoos analogon to the map function from
 * functional programming word.
 * 
 * @author nono
 * 
 */
public class FunctorMapper implements Mapper {

	private Mapper mapper;

	public FunctorMapper(Mapper m) {
		this.mapper = m;
	}

	public FunctorMapper() {
	}

	/**
	 * @return Returns the mapper.
	 */
	public Mapper getMapper() {
		return mapper;
	}

	/**
	 * @param mapper
	 *            The mapper to set.
	 */
	public void setMapper(Mapper mapper) {
		this.mapper = mapper;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.Mapper#map(java.lang.Object, java.util.Map)
	 */
	public Object map(Object input, Map param) throws MapperException {
		List mr = new ArrayList();
		for (Iterator it = ((Collection) input).iterator(); it.hasNext();) {
			Object o = mapper.map(it.next(), param);
			mr.add(o);
		}
		return mr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.Mapper#getName()
	 */
	public String getName() {
		return "functor";
	}

}
